package engine.processing.data.pigengine;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.pig.PigServer;

public class PigMR {

	private PigServer pigServer;

	public PigMR(final boolean isLocal) throws Exception {
		if (isLocal) {
			pigServer = new PigServer("local");
		} else {
			pigServer = new PigServer("mapreduce");
		}
		pigServer.debugOn();
// Just in case if you use a Pig UDF
//		pigServer.registerJar("pig-reporting-1.0-SNAPSHOT-jar-with-dependencies.jar");
	}

	public void runIdQuery(final List<String> queries,
			final Map<String, String> dumpDataMap, final String destPathPrefix)
			throws IOException {

		for (String q : queries) {
			System.out.println("Loading query : "+q);
			pigServer.registerQuery(q);
		}
		System.out.println("Pig Server Alias Key : "+pigServer.getAliasKeySet());
		Set<String> dumpKeys = dumpDataMap.keySet();
		for (String k : dumpKeys) {
			System.out.println("Dumping DataSet : " + k);
			pigServer.store(dumpDataMap.get(k), destPathPrefix + "-"
					+ dumpDataMap.get(k));
		}
	}
}
